package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.IGoodsService;
import cn.stylefeng.guns.dao.entity.Goods;
import cn.stylefeng.guns.dao.service.IGoodsGroupRelationService;
import cn.stylefeng.guns.dao.service.IGoodsGroupService;
import cn.stylefeng.guns.modular.demo.controller.domain.GoodsQueryRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.VenderVO;
import cn.stylefeng.guns.modular.demo.excel.domain.GoodsExcelVO;
import cn.stylefeng.guns.modular.demo.excel.listener.GoodsListener;
import cn.stylefeng.guns.modular.demo.excel.listener.SupplierListener;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gettyio.core.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-01-25
 */
@RestController
@Slf4j
@ApiResource(name = "商品", resBizType = ResBizTypeEnum.BUSINESS, path = "/goods")
public class GoodsController {
    @Resource
    private IGoodsService goodsService;

    @Resource
    private IGoodsGroupService goodsGroupService;
    @Resource
    private IGoodsGroupRelationService goodsGroupRelationService;


    /**
     * 示例方法
     *
     * @author fengshuonan
     * @since 2021/1/24 10:59
     */
    @PostResource(name = "商品列表", path = "/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("商品列表")
    public ResponseData<PageResult<Goods>> list(@RequestBody GoodsQueryRequest queryRuleRequest) {

        Page<Goods> page = new Page<>();
        page.setCurrent(queryRuleRequest.getPageNo());
        page.setSize(queryRuleRequest.getPageSize());
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = Wrappers.<Goods>lambdaQuery()
                .eq(!StringUtils.isEmpty(queryRuleRequest.getCommodityCode()), Goods::getCommodityCode, queryRuleRequest.getCommodityCode());
//                .like(!StringUtils.isEmpty(queryRuleRequest.getGoodsName()), Goods::getCommodityName, queryRuleRequest.getGoodsName()

        if (!StringUtils.isEmpty(queryRuleRequest.getGoodsName())) {
            lambdaQueryWrapper.and(r -> r.like(Goods::getCommodityName, queryRuleRequest.getGoodsName()).or().like(Goods::getCommodityCode, queryRuleRequest.getGoodsName()));
        }

        if (!StringUtils.isEmpty(queryRuleRequest.getPym())) {
            lambdaQueryWrapper.and(r -> r.like(Goods::getCommodityName, queryRuleRequest.getPym())
                    .or()
                    .like(Goods::getCommodityCode, queryRuleRequest.getPym())
                    .or()
                    .like(Goods::getProPym, queryRuleRequest.getPym())
            );
        }


        Page<Goods> page1 = goodsService.page(page, lambdaQueryWrapper);
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1));
    }


    @PostResource(name = "厂家列表", path = "/vender/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("厂家列表")
    public ResponseData<List<VenderVO>> listVender(@RequestBody GoodsQueryRequest queryRuleRequest) {

        Page<Goods> page = new Page<>();
        page.setCurrent(queryRuleRequest.getPageNo());
        page.setSize(queryRuleRequest.getPageSize());
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = Wrappers.<Goods>lambdaQuery().select(Goods::getVender).isNotNull(Goods::getVender)
                .not(r -> r.eq(Goods::getVender, StringUtil.EMPTY_STRING))
                .groupBy(Goods::getVender);
        List<Goods> list = goodsService.list(lambdaQueryWrapper);
        return new SuccessResponseData<>(list.stream().map(r -> {
            VenderVO venderVO = new VenderVO();
            venderVO.setVender(r.getVender());
            return venderVO;
        }).collect(Collectors.toList()));
    }




    @ResponseBody
    @PostResource(name = "商品导入", path = "/upload", requiredLogin = true, requiredPermission = true)
    @ApiOperation("商品导入")
    @BusinessLog
    public ResponseData<String> upload(MultipartFile file) throws IOException {
        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), GoodsExcelVO.class, new GoodsListener(goodsService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }



}
